package com.soulkun.smarthome.ui.main.home

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.tabs.TabLayoutMediator
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.header.ClassicsHeader
import com.soulkun.smarthome.R
import com.soulkun.smarthome.databinding.AppFragmentMainHomeDeviceHistoryBinding
import com.soulkun.smarthome.databinding.AppFragmentMainHomeDeviceHistoryCommandBinding
import com.soulkun.smarthome.databinding.AppFragmentMainHomeDeviceRecentBinding
import com.soulkun.smarthome.ui.main.home.adapter.AppMainHomeDeviceHistoryCommandListAdapter
import com.soulkun.smarthome.ui.main.home.viewmodel.AppMainHomeDeviceViewModel
import com.soulkun.smarthome.ui.main.home.viewmodel.AppMainHomeViewModel
import soulkun.library.helper.decoration.LinearSpaceItemDecorationHelper
import soulkun.library.mvvm.component.BaseDataBindingFragment

class AppMainHomeDeviceHistoryCommandFragment(private val viewModel: AppMainHomeDeviceViewModel) :
    BaseDataBindingFragment<AppFragmentMainHomeDeviceHistoryCommandBinding>() {

    private val adapter by lazy {
        AppMainHomeDeviceHistoryCommandListAdapter(
            mContext,
            viewModel.deviceCommandHistory.commandHistoryList
        )
    }

    override fun setLayout(): Int {
        return R.layout.app_fragment_main_home_device_history_command
    }

    override fun initView() {
        requireDataBinding().srlMainHomeDeviceHistoryCommandContainerHistory.run {
            setRefreshHeader(ClassicsHeader(mContext))
            setRefreshFooter(ClassicsFooter(mContext))
            setOnRefreshListener {
                viewModel.deviceCommandHistory.cursor = null
                viewModel.getDeviceCommandHistory {
                    finishRefresh()
                }
            }
            setOnLoadMoreListener {
                if (viewModel.deviceCommandHistory.cursor == null) {
                    finishLoadMoreWithNoMoreData()
                } else {
                    viewModel.getDeviceCommandHistory {
                        finishLoadMore()
                    }
                }
            }
        }
        requireDataBinding().rvMainHomeDeviceHistoryCommandListHistory.run {
            layoutManager = LinearLayoutManager(mContext)
            adapter = this@AppMainHomeDeviceHistoryCommandFragment.adapter
            if (itemDecorationCount == 0) {
                addItemDecoration(LinearSpaceItemDecorationHelper(0, 10, 15, 15, 10))
            }
        }
    }

    override fun initData() {
        viewModel.getDeviceCommandHistory {
        }
    }

    override fun initListener() {
    }

    override fun doOnCreate() {
    }

}